package com.dream.base.ui;

import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;

import com.dream.base.R;
import com.dream.base.tools.LogUtil;
import com.dream.base.tools.ToastUtil;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadmoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.concurrent.TimeUnit;

import io.reactivex.Observable;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

/**
 * @Description: 带有下拉和上拉刷新功能的fragment
 * @author: Wang
 * @date: 2018/1/4 14:03
 * @email: life_artist@163.com
 * @Copyright©2018 by wang. All rights reserved.
 */

public abstract class RefreshFragment extends BaseFragment {

    protected SmartRefreshLayout mRefreshContainer;

    protected View mEmptyContainer;

    protected RecyclerView mDataContainer;

    private Button testButton;

    private OnRefreshListener onRefreshListener;

    private OnLoadmoreListener onLoadmoreListener;

    @Override
    protected int setViewLayout() {
        return R.layout.fragment_refresh_view;
    }

    @Override
    protected void findViews(View view) {
        mRefreshContainer = (SmartRefreshLayout) view.findViewById(R.id.refresh_data_layout);
        mEmptyContainer = view.findViewById(R.id.empty_container);
        testButton = (Button) view.findViewById(R.id.empty_view_container);
        mDataContainer = (RecyclerView) view.findViewById(R.id.data_container);
    }

    @Override
    protected void initViews() {


        testButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mEmptyContainer.setVisibility(View.GONE);
                mRefreshContainer.autoRefresh();
            }
        });

        onRefreshListener = new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshLayout) {
                testRefresh();
            }
        };
        onLoadmoreListener = new OnLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshLayout) {
                testLoadMore();
            }
        };
        mRefreshContainer.setOnRefreshListener(onRefreshListener);
        mRefreshContainer.setOnLoadmoreListener(onLoadmoreListener);
    }

    /**
     * 触发下拉刷新
     */
    protected abstract void onRefresh();

    /**
     * 触发加载更多
     */
    protected abstract void onLoadMore();

    private void testLoadMore() {
        Observable
                .interval(2, TimeUnit.SECONDS)
                .take(1)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<Long>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(Long value) {

                    }

                    @Override
                    public void onError(Throwable e) {
                        LogUtil.i(TAG, "");
                    }

                    @Override
                    public void onComplete() {
                        ToastUtil.showShort(getActivity(), "刷新失败");
                        mRefreshContainer.finishLoadmore(false);
                        mEmptyContainer.setVisibility(View.VISIBLE);
                    }
                });
    }

    private void testRefresh() {

        mEmptyContainer.setVisibility(View.GONE);
        Observable
                .interval(2, TimeUnit.SECONDS)
                .take(1)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<Long>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(Long value) {

                    }

                    @Override
                    public void onError(Throwable e) {
                        LogUtil.i(TAG, "");
                    }

                    @Override
                    public void onComplete() {
                        ToastUtil.showShort(getActivity(), "刷新成功");
                        mRefreshContainer.finishRefresh(true);
                    }
                });
    }
}
